package JAVAcollectionsanddatastructures.binaryTree;


import java.util.*;

//根据一个前序遍历创建一棵二叉树,在输出它的中序遍历
class TreeNode1 {
    public char value;
    public TreeNode1 left;
    public TreeNode1 right;

    public TreeNode1(char value) {
        this.value = value;
    }
}

public class createbinaryTree {
    public static int i = 0;

    public static TreeNode1 createTree(String str) {
        TreeNode1 root = null;
        if (str.charAt(i) != '#') {
            root = new TreeNode1(str.charAt(i));
            i++;
            root.left = createTree(str);
            root.right = createTree(str);
        } else {
            //遇到#时为空树
            i++;
        }
        return root;
    }

    public static void inorder(TreeNode1 root) {
        if (root == null) {
            return;
        }
        inorder(root.left);
        System.out.print(root.value + " ");
        inorder(root.right);
    }


    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.println("请输入前序遍历为：");
        while (in.hasNextLine()) {
            String str = in.nextLine();
            TreeNode1 root = createTree(str);
            System.out.println("所得中序遍历为：");
            inorder(root);
        }
    }
}
